<template>
  <div class="login-container">
    <div class="login_box">
      <el-tooltip class="item" effect="dark" :content="content" placement="top-end">
        <!-- 扫码登录 -->
        <div class="ercode" @click="witching_mode">
          <img v-if="switch_login_mode" src="@/assets/images/ercode.png" alt="" />
          <img v-else src="@/assets/images/computer.png" alt="" />
        </div>
      </el-tooltip>

      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
        label-position="left">
        <template v-if="switch_login_mode">
          <div class="title-container">
            <h3 class="title">企达驾校管理系统欢迎您！</h3>
          </div>

          <el-form-item prop="userno">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input ref="userno" v-model="loginForm.userno" placeholder="请输入用户名" name="userno" type="text"
              tabindex="1" auto-complete="on" />
          </el-form-item>

          <el-form-item prop="passwd">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input :key="passwordType" ref="password" v-model="loginForm.passwd" :type="passwordType"
              placeholder="请输入密码x" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <div class="checkbox">
            <el-checkbox v-model="checked">7天内自动登录</el-checkbox>
            <span>忘记密码</span>
          </div>
          <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px"
            @click.native.prevent="handleLogin">登录</el-button>
        </template>

        <template v-else>
          <div class="QRcode">
            <p>打开微信扫一扫</p>
            <div style="display: flex; justify-content: center">
              <img src="@/assets/images/微信图片_20231030170757.jpg" alt="" />
            </div>
          </div>
        </template>
      </el-form>
    </div>
  </div>
</template>

<script>
// 角色权限
import { validUsername } from "@/utils/validate";
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      /* if (!validUsername(value)) {
        callback(new Error('请输入账户'))
      } else {
        callback()
      } */

      if (!value) {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length == 0) {
        callback(new Error("请输入密码"));
      } else if (value.length < 3) {
        callback(new Error("密码长度不能小于3"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        action: "login",
        userno: "",
        passwd: "",
      },
      loginRules: {
        userno: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        passwd: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      checked: true,
      content: "点击切换扫码快捷登录",
      switch_login_mode: true,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  mounted() {
    let rgba = JSON.parse(localStorage.getItem('Back'))
    let item = JSON.parse(localStorage.getItem('ColorIndex'))?.val
    rgba && this.$store.commit('app/SET_BACKGROUND', rgba)
    item && this.$store.commit('app/SET_COLOR', item)
  },
  methods: {
    // 切换登录方式
    witching_mode() {
      this.switch_login_mode = !this.switch_login_mode;
      this.switch_login_mode
        ? (this.content = "点击切换扫码快捷登录")
        : (this.content = "点击切换账户密码登录");
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;

          // 登录逻辑
          this.$store.dispatch("user/login", this.loginForm).then(() => {
            // this.$router.push({ path: this.redirect || "/" });//返回到上一个页面
            this.$router.push({ path:"/" });//跳转到首页/
            this.loading = false;
          })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #333;
$cursor: #333;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 100000000px white inset !important; //解决快速填充账号密码输入框变黑问题
        -webkit-text-fill-color: $cursor !important;
      }

    }
  }

  .el-form-item {
    border: 1px solid #e6e6e6;
    background: white;
    border-radius: 5px;
    color: #333;
    margin-bottom: 22px !important;
  }
}
</style>

<style lang="scss" scoped>
// $bg: #2d3a4b;
$bg: #f2f7ff;
$dark_gray: #666;
$light_gray: #333;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  height: 100vh;

  .login_box {
    margin: 0 auto;
    width: 600px;
    height: 500px;
    display: flex;
    justify-content: center;
    padding: 80px 70px 0;
    margin-top: 100px;
    border-radius: 12px;
    background-color: #fff;
    position: relative;
    box-shadow: 0 1px 32px 0 #e8e8e8;

    .QRcode {
      display: flex;
      justify-content: center;
      flex-direction: column;

      p {
        text-align: center;
        font-size: 16px;
        font-weight: 400;
        color: #333;
        margin-bottom: 50px;
      }

      img {
        width: 200px;
        height: 200px;
      }
    }

    .ercode {
      cursor: pointer;
      background-size: auto;
      background-size: 100% 100%;
      position: absolute;
      right: 0;
      top: 0;

      img {
        width: 80px;
        height: 80px;
      }
    }
  }

  .checkbox {
    margin-bottom: 22px;
    display: flex;
    justify-content: space-between;

    ::v-deep .el-checkbox__label:hover {
      color: #409eff;
    }

    span {
      color: #606266;
      font-weight: 500;
      font-size: 14px;
      cursor: pointer;
    }

    span:hover {
      color: #409eff;
    }
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
